/*
Copyright SecureKey Technologies Inc. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/

package connector

import (
	"git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/ichain/events/client/inoderesolver"
	"git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/ichain/events/client/inoderesolver/balanced"
	"git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/ichain/events/client/inoderesolver/preferorg"
	"git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/providers/context"
	"git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/providers/ichain"
)

type params struct {
	// inodeResolverProvider 选择节点策略
	inodeResolverProvider inoderesolver.Provider
}

func defaultParams(context context.Client, chainID string) *params {
	// 默认PreferOrgStrategy
	policy := context.EndpointConfig().ChainConfig(chainID).Policies.EventService
	return &params{
		inodeResolverProvider: getInodeResolver(policy),
	}
}

func getInodeResolver(policy ichain.EventServicePolicy) inoderesolver.Provider {
	switch policy.ResolverStrategy {
	case ichain.PreferOrgStrategy, ichain.MinBlockHeightStrategy:
		logger.Debugf("Using prefer-org inode resolver")
		return preferorg.NewResolver()
	case ichain.BalancedStrategy:
		logger.Debugf("Using balanced inode resolver")
		return balanced.NewResolver()
	default:
		logger.Debugf("Resolver strategy not specified. Using prefer-org inode resolver.")
		return preferorg.NewResolver()
	}
}
